package cn.cloudtogo.common.model;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

/**
 * @author yaolianhua789@gmail.com
 **/
public class ExternalDatasourceRowMapper implements RowMapper<ExternalDatasourceModel> {

    @Override
    public ExternalDatasourceModel mapRow(ResultSet rs, int rowNum) throws SQLException {
        ExternalDatasourceModel ed = new ExternalDatasourceModel();

        ed.setUuid(rs.getString("_uuid"));
        ed.setHost(rs.getString("host"));
        ed.setPort(rs.getInt("port"));
        ed.setUsername(rs.getString("username"));
        ed.setPassword(rs.getString("password"));
        ed.setUrl(rs.getString("url"));
        ed.setProject(rs.getString("project"));
        ed.setSchema(rs.getString("_schema"));
        ed.setName(rs.getString("name"));
        ed.setOpenid(rs.getString("openid"));
        ed.setType(DatasourceTypes.valueOf(rs.getString("type")));
        Timestamp createdAt = rs.getTimestamp("created_at");
        ed.setCreateAt(createdAt == null ? null : createdAt.toLocalDateTime());

        return ed;
    }
}
